import {nextTick, onMounted} from 'vue'
import {
    fetchBoothList,
    fetchBoothUnbindProduct,
    fetchBoothUnbindAdvert,
    boothBind,
    updateBoothStatus
} from '@/api/homeManage'
import {selectInstitution, fetchProductSelect, selectProductList} from '@/api/index'
import {ElMessage, ElMessageBox} from "element-plus";

export default function (state, ref) {
    const methods = {
        // 获取列表
        async getTableData() {
            const res = await fetchBoothList(state.page)
            if (res.code === 200) {
                const {list, total} = res.data
                state.tableData = list
                state.page = {
                    ...state.page,
                    total
                }
            }
        },
        /**
         * 切换状态
         */
        switchChange({id, status}) {
            ElMessageBox.confirm('确认进行此操作?', '提示', {
                type: 'warning',
            }).then(async () => {
                const res = await updateBoothStatus({
                    status: status === 0 ? 1 : 0,
                    id
                })
                if (res.code === 200) {
                    ElMessage.success('操作成功')
                    methods.getTableData()
                }
            }).catch(() => {

            });
        },
        async getBindProduct() {

        },
        async getBindBannar() {

        },
        async getBannar() {
            const res = await fetchBoothUnbindAdvert({
                boothId: state.tableRows.id,
                appId: state.tableRows.appId,
                ...state.bannarPage
            })
            if (res.code === 200) {
                state.bannarVisible.isShow = true
                state.bannarVisible.title = state.tableRows.title + '绑定广告'
                state.bannarVisible.tableData = res.data
                nextTick(() => {
                    state.bannarVisible.selectRows = res.data.filter(item => item.isOk === 1)
                    state.bannarVisible.selectRows.forEach(item => {
                        ref.bannarRef.value.$refs.table.toggleRowSelection(item, true)
                    })
                })
            }
        },
        async getProduct() {
            const res = await fetchBoothUnbindProduct({
                boothId: state.tableRows.id,
                appId: state.tableRows.appId,
                ...state.productPage
            })
            if (res.code === 200) {
                state.productVisible.isShow = true
                state.productVisible.title = state.tableRows.title + '绑定产品'
                state.productVisible.tableData = res.data
                nextTick(() => {
                    state.productVisible.selectRows = res.data.filter(item => item.isOk === 1)
                    state.productVisible.selectRows.forEach(item => {
                        ref.productRef.value.$refs.table.toggleRowSelection(item, true)
                    })
                })
            }
        },
        // 获取未绑定广告列表
        async getUnboundAdvert(row) {
            state.tableRows = {...row}
            methods.getBannar()
        },
        // 获取未绑定产品列表
        getUnboundProduct(row) {
            state.tableRows = {...row}
            methods.getProduct()
            fetchProductSelect().then(res => {
                if (res.code === 200) state.productVisible.productList = res.data
            })
            selectInstitution().then(res => {
                if (res.code === 200) state.productVisible.institutionList = res.data
            })
        },
        /**
         * 绑定广告提交
         */
        async bindBannarConfirm() {
            // if (state.bannarVisible.selectRows.length <= 0) return ElMessage.error('请先选择广告')
            let selectRows = state.bannarVisible.selectRows.map(item => {
                return {
                    "bindId": item.id,
                    "boothId": state.tableRows.id,
                    "type": item.title === "banner" ? 0 : 2,
                    'appName': item.appName,
                    'appId': item.appId,
                }
            })
            const res = await boothBind(selectRows.length > 0 ? selectRows : [{boothId: state.tableRows.id,}])
            if (res.code === 200) {
                ElMessage.success('绑定成功')
                state.bannarVisible.isShow = false
            }
        },
        /**
         * 绑定产品提交
         */
        async bindProductConfirm() {
            let selectRows = state.productVisible.selectRows.map(item => {
                return {
                    "bindId": item.productId,
                    "boothId": state.tableRows.id,
                    "type": 1,
                    'appName': item.appName,
                    'appId': item.appId,
                }
            })
            const res = await boothBind(selectRows.length > 0 ? selectRows : [{boothId: state.tableRows.id,}])
            if (res.code === 200) {
                ElMessage.success('绑定成功')
                state.productVisible.isShow = false
            }
        },
        /***
         * 获取已绑定列表
         */
        // async bindListShow(row) {
        //     if (row.type === 1) {
        //         const productData = await methods.getProductList()
        //         const bind = await methods.getBindList(row)
        //         //产品已绑
        //         state.bindProductVisible = {
        //             isShow: true,
        //             title: row.title + ' - ' + '已绑列表'
        //         }
        //
        //     } else {
        //         const advertData = await methods.getAdvertList()
        //         const bind = await methods.getBindList(row)
        //         //  banner、广告已绑
        //         state.bindBannarVisible = {
        //             isShow: true,
        //             title: row.title + ' - ' + '已绑列表'
        //         }
        //     }
        //
        // },
        // async getBindList(row) {
        //     const res = await boothBindProduct({
        //         boothId: row.id
        //     })
        //     if (res.code === 200) {
        //         return res.data
        //     }
        // },
        // async getAdvertList() {
        //     const res = await fetchAdvert({pageNum: 1, pageSize: 1000})
        //     if (res.code === 200) {
        //         return res.data.list
        //     }
        // },
        // async getProductList() {
        //     const res = await fetchProduct({pageNum: 1, pageSize: 1000})
        //     if (res.code === 200) {
        //         return res.data.list
        //     }
        // },
        // 批量删除
        // batchDelete() {
        //     if (state.advertSelectionRows.length <= 0) return ElMessage.error('请先勾选要删除的展位')
        //     ElMessageBox.confirm('确认删除?', '提示', {
        //         type: 'warning',
        //     }).then(async () => {
        //         const ids = state.advertSelectionRows.map(item => {
        //             return {
        //                 id: item.id,
        //                 status: 0
        //             }
        //         })
        //         const res = await batchDeleteAdvert(ids)
        //         if (res.code === 200) {
        //             ElMessage.success('删除成功')
        //             methods.getTableData()
        //         }
        //     }).catch(() => {
        //
        //     });
        // },
        resetSearch() {
            state.page = {
                pageNum: 1,
                pageSize: 10,
                total: 0
            }
            methods.getTableData()
        },
        bannarResetSearch() {
            state.bannarPage = {}
            methods.getBannar()
        },
        closeBannarClear() {
            state.bannarPage = {}
        },
        closeProductClear() {
            state.productPage = {}
        },
        productResetSearch() {
            state.productPage = {}
            methods.getProduct()
        }
    }
    onMounted(() => {
        methods.getTableData()
        selectProductList().then(res => {
            if (res.code === 200) {
                state.loanProductList = res.data
            }
        })
    })
    return methods
}
